<%@page import="com.ddd.entity.Contract"%>
<%@page import="com.ddd.entity.Publisher"%>
<%@page import="com.ddd.context.Context"%>
<%@page import="com.ddd.entity.Journal"%>
<%@page import="com.ddd.service.JournalService"%>
<%@page import="com.ddd.service.ContractService"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.ddd.service.PublisherService"%>
<%@page import="com.ddd.util.Util"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    Util.checkBEAuthorized(request, response);

    PublisherService publisherService = new PublisherService(request);
    ArrayList publisherList = publisherService.loadAll();

    ContractService contractService = new ContractService(request);
    ArrayList contractList = contractService.loadAll();

    JournalService journalService = new JournalService(request);

    String mode = request.getParameter("mode");
    String id = request.getParameter("id");
    Journal journal = new Journal();
    if (Util.isBlank(mode)) {
        mode = "add";
    } else if (!mode.equals("add")) {
        journal = journalService.load(id);
    }

    String message = request.getParameter("message");
%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=Context.SITE_NAME%></title>

        <link type="text/css" href="css/style.css" rel="stylesheet" />
        <link type="text/css" href="css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
        <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
        <script type="text/javascript">
            $(function() {
                var availableLanguages = [
                    "中文 (繁體)",
                    "中文 (簡體)",
                    "英文"
                ];
                
                $( "#languageSN" ).autocomplete({
                    source: availableLanguages
                });
            });
            
            function doUpdate (){
                //$( "#dialog:ui-dialog" ).dialog( "destroy" );
                $( "#dialog_update" ).dialog({
                    title: "更新期刊",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "更新": function() {
                            $( this ).dialog( "close" );
                            $('#form1').submit();
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            }
            
            /* 
            function doDelete (){
                //$( "#dialog:ui-dialog" ).dialog( "destroy" );
                $( "#dialog_delete" ).dialog({
                    title: "刪除期刊",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "刪除": function() {
                            $( this ).dialog( "close" );
                            $('#mode').val("delete");
                            $('#form1').submit();
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            }*/
            
            function showMessage(){
            <%if (Util.isNotBlank(message)) {%>
                    $( "#dialog_message" ).dialog({
                        modal: true,
                        title:"訊息",
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
            <%}%>
                }
            
        </script>
    </head>
    <body onload="showMessage();">
        <div id="wrapper">
            <div id="header">
                <div id="logo">
                    <h1><a href="#"><%=Context.SITE_NAME%></a></h1>
                </div>
            </div>
            <!-- end #header -->
            <div id="menu">
                <%@ include file="menu.jsp" %>
            </div>
            <!-- end #menu -->
            <div id="page">
                <div id="page-bgtop">
                    <div id="page-bgbtm">
                        <!-- starrt #content -->
                        <div id="content">
                            <form id="form1" name="form1" action="../doJournal" method="post">
                                <input type="hidden" id="mode" name="mode" value="<%=mode%>"></input>
                                <input type="hidden" id="cid" name="cid" value="<%=journal.getCid()%>"></input>
                                <input type="hidden" id="ctime" name="ctime" value="<%=journal.getCtime()%>"></input>
                                <input type="hidden" id="uid" name="uid" value="<%=journal.getUid()%>"></input>
                                <input type="hidden" id="utime" name="utime" value="<%=journal.getUtime()%>"></input>
                                <table>
                                    <tr>
                                        <td align="left">Journal ID</td>
                                        <%if (mode.equals("update")) {%>
                                        <td><input id="id" name="id" value="<%=journal.getId()%>" readonly></td>
                                            <%} else {%>
                                        <td><input id="id" name="id" value="<%=journal.getId()%>"></td>
                                            <%}%>
                                    </tr>
                                    <tr>
                                        <td align="left">期刊名稱</td>
                                        <td><input id="name" name="name" value="<%=journal.getName()%>"></td>
                                    </tr>
                                    <tr>
                                        <td align="left">出版單位</td>
                                        <td>
                                            <select id="fkPublisherID" name="fkPublisherID">
                                                <% for (int i = 0; i < publisherList.size(); i++) {
                                                        Publisher p = (Publisher) publisherList.get(i);
                                                        if (journal.getFkPublisherId().equals(p.getId())) {
                                                %>
                                                <option value="<%=p.getId()%>" selected="<%=p.getId()%>"><%=p.getName()%></option>
                                                <%} else {%>
                                                <option value="<%=p.getId()%>"><%=p.getName()%></option>
                                                <%}
                                                    }%>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">出版週期</td>
                                        <td><input id="frequency" name="frequency" value="<%=journal.getFrequency()%>"></td>
                                    </tr>
                                    <tr>
                                        <td align="left">創刊年份</td>
                                        <td><input id="foundYear" name="foundYear" size="4" value="<%=journal.getFoundYear()%>"></td>
                                    </tr>
                                    <tr>
                                        <td align="left">語言</td>
                                        <td><input id="languageSN" name="languageSN" value="<%=journal.getLanguageSn()%>"></td>
                                    </tr>
                                    <tr>
                                        <td align="left">ISSN</td>
                                        <td><input id="ISSN" name="ISSN" size="9" value="<%=journal.getIssn()%> "></td>
                                    </tr>
                                    <tr>
                                        <td align="left">Embargo</td>
                                        <%if (Util.isNotBlank(journal.getEmbargo()) && journal.getEmbargo().equals("on")) {%>
                                        <td><input id="embargo" name="embargo" type="checkbox" checked></td>
                                            <%} else {%>
                                        <td><input id="embargo" name="embargo" type="checkbox"></td>
                                            <%}%>
                                    </tr>
                                    <tr>
                                        <td align="left">延期數</td>
                                        <td><input id="embargoFreq" name="embargoFreq" size="2" value="<%=journal.getEmbargoFreq()%>"></td>
                                    </tr>
                                    <tr>
                                        <td align="left">網址</td>
                                        <td><input id="oaLink" name="oaLink" value="<%=journal.getOaLink()%>"></td>
                                    </tr>
                                    <tr>
                                        <td align="left">期刊封面</td>
                                        <td><input id="picture" name="picture" value="<%=journal.getPicture()%>"></td>
                                    </tr>
                                    <tr>
                                        <td align="left">開始卷</td>
                                        <td><input id="beginVolume" name="beginVolume" value="<%=journal.getBeginVolume()%>"></td>
                                    </tr>
                                    <tr>
                                        <td align="left">開始期</td>
                                        <td><input id="beginIssue" name="beginIssue" value="<%=journal.getBeginIssue()%>"></td>
                                    </tr>
                                    <tr>
                                        <td align="left">全部授權</td>
                                        <%if (Util.isNotBlank(journal.getAuthorized()) && journal.getAuthorized().equals("on")) {%>
                                        <td><input id="authorized" name="authorized" type="checkbox" checked></td>
                                            <%} else {%>
                                        <td><input id="authorized" name="authorized" type="checkbox"></td>
                                            <%}%>
                                    </tr>
                                    <tr>
                                        <td align="left">現刊%</td>
                                        <td><input id="currCommission" name="currCommission" value="<%=journal.getCurrCommission()%>"></td>
                                    </tr>
                                    <tr>
                                        <td align="left">過刊%</td>
                                        <td><input id="pastCommission" name="pastCommission" value="<%=journal.getPastCommission()%>"></td>
                                    </tr>
                                    <tr>
                                        <td align="left">合約編號</td>
                                        <td>
                                            <select id="fkContractID" name="fkContractID">
                                                <% for (int i = 0; i < contractList.size(); i++) {
                                                        Contract c = (Contract) contractList.get(i);
                                                        if (journal.getFkContractId().equals(c.getId())) {
                                                %>
                                                <option value="<%=c.getId()%>" selected="<%=c.getId()%>"><%=c.getId()%></option>
                                                <%} else {%>
                                                <option value="<%=c.getId()%>"><%=c.getId()%></option>
                                                <%}
                                                    }%>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">備註</td>
                                        <td><textarea id="note" name="note" cols="30" rows="5"><%=journal.getNote()%></textarea></td>
                                    </tr>
                                    <tr>
                                        <%if (mode.equals("update")) {%>
                                        <td colspan="2" align="right"> 
                                            <input type="button" value="更新" onclick="doUpdate();" />
                                        </td>
                                        <%} else {%>
                                        <td colspan="2" align="right"> 
                                            <input type="submit" value="新增"/>
                                        </td>
                                        <%}%>
                                    </tr>
                                </table>

                            </form>
                            <div style="clear: both;">&nbsp;</div>
                        </div>
                        <!-- end #content -->
                        <div style="clear: both;">&nbsp;</div>
                    </div>
                </div>
            </div>
            <!-- end #page -->
        </div>
        <div id="footer">
            <p><%=Context.FOOTER%></p>
        </div>
        <!-- end #footer -->
        <!-- ui-dialog -->
        <div id="dialog_update" style="display: none">
            <p>是否要更新期刊？</p>
        </div>

        <div id="dialog_message" style="display: none;">
            <p><%=message%></p>
        </div>

    </body>
</html>